from langchain_community.chat_models import ChatZhipuAI
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_text_splitters import RecursiveCharacterTextSplitter


import os

os.environ['ZHIPUAI_API_KEY']="e07484c365a3d8b468a4d9e06b36317a.momrwta4EftapAwx"

chat = ChatZhipuAI(
    model='glm-3-turbo',
    temperature=0.5,
)

messages = [
    AIMessage(content='Hi'),
    SystemMessage(content='Your role is a poet.'),
    HumanMessage(content='Write a song about a happy carrot.')
]

#response = chat.invoke(messages)
#print(response.content)

#exit()

with open('news1.txt') as f:
    news_content = f.read()

text_splitter=RecursiveCharacterTextSplitter(
    chunk_size=100,
    chunk_overlap=20,
    length_function=len
)

texts = text_splitter.create_documents([news_content])
#print(texts[0])
#print(texts[1])
#exit()


from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template('总结以下内容：{context}')
chain = create_stuff_documents_chain(chat, prompt)
#result = chain.invoke({'context': texts})
#print(result)
#exit()


from langchain_core.prompts import PromptTemplate

with open('summarize.txt') as f:
    summary_cmd = f.read()

prompt = PromptTemplate.from_template(summary_cmd)

chain = prompt | chat
result = chain.invoke({'text': news_content})
print(result.content)

